package com.hns.can.safety.web;

import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.common.domain.GridPage;
import com.hns.can.common.entity.BaseEntity;
import com.hns.can.common.web.BaseController;
import com.hns.can.safety.service.BaseLineBlackRelService;
import com.hns.can.safety.vo.BaseLineBlackRelVo;
import com.hns.can.safety.vo.BlackSpotLineVo;
import com.hns.frame.exception.BusinessException;
import com.hns.frame.mybatis.Pagination;
import com.hns.tool.export.ExportUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 线路黑点分配Controller
 *
 * @version Revision 2.0.0
 * @author:jeffxu
 * @email:
 */
@Controller
@RequestMapping("/baseLineBlackRel")
public class BaseLineBlackRelController extends BaseController {

    @Autowired
    private BaseLineBlackRelService baseLineBlackRelService;

    //文件表头
    private final String[] tableHeaderNames = {"线路名称", "黑点名称", "黑点危险度", "状态", "启用/停用日期"};
    //表头对应字段
    private final String[] tableHeaderFields = {"lineName", "blackName", "dangerDegree", "blackSt", "mnpltDate"};


    /**
     * 获取线路分配黑点列表（分页）
     *
     * @param vo
     * @return
     * @throws Exception
     */
    @RequestMapping("/searchBaseLineBlackRelPage")
    @ResponseBody
    public Object searchBaseLineBlackRelPage(BaseLineBlackRelVo vo) throws Exception {
        Pagination page = baseLineBlackRelService.searchBaseLineBlackRelPage(vo);
        GridPage grid = new GridPage(page);
        return this.getReturnResult(grid);
    }

    /**
     * 添加线路分配黑点
     *
     * @param vo
     * @return
     * @throws Exception
     */
    @RequestMapping("/addBaseLineBlackRel")
    @ResponseBody
    public Object addBaseLineBlackRel(BaseLineBlackRelVo vo) throws Exception {
        SysUserEntity user = this.getCurrentUser().getUser();

        baseLineBlackRelService.addBaseLineBlackRel(vo, user);
        return this.getReturnResult();
    }

    /**
     * 变更黑点状态（启用、停用）
     *
     * @param relId
     * @return
     * @throws BusinessException
     */
    @RequestMapping("/openCloseLineBlackRelById")
    @ResponseBody
    public Object openCloseLineBlackRelById(String relId) throws BusinessException {
        SysUserEntity user = this.getCurrentUser().getUser();

        baseLineBlackRelService.openCloseLineBlackRelById(user, relId);
        return this.getReturnResult();
    }

    /**
     * 获取线路下的所有黑点信息，用于展示在地图上
     *
     * @param vo
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping("/getBlackSpotByLineId")
    @ResponseBody
    public Object getBlackSpotByLineId(
            BaseLineBlackRelVo vo, HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<BaseEntity> list = baseLineBlackRelService.getBlackSpotByLineId(vo);
        return this.getReturnResult(list);
    }

    /**
     * 获取勾选的黑点信息，用于展示在地图上
     *
     * @param vo
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping("/getBlackSpotsByIds")
    @ResponseBody
    public Object getBlackSpotsByIds(
            String[] ids, HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<BaseEntity> list = baseLineBlackRelService.getBlackSpotsByIds(ids);
        return this.getReturnResult(list);
    }

    /**
     * 导出excel
     *
     * @param vo
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping("/exportBaseLineBlackRelExcel")
    public void exportBaseLineBlackRelExcel(
            BaseLineBlackRelVo vo, HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<BaseEntity> list = baseLineBlackRelService.getBaseLineBlackRelList(vo);
        ExportUtil.exportFiles(Arrays.asList(tableHeaderNames), Arrays.asList(tableHeaderFields), list, response, "excel", "baseLineBlackRel", "线路黑点分配信息");
    }

    /**
     * 导出pdf
     *
     * @param vo
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping("/exportBaseLineBlackRelPdf")
    public void exportBaseLineBlackRelPdf(
            BaseLineBlackRelVo vo, HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<BaseEntity> list = baseLineBlackRelService.getBaseLineBlackRelList(vo);
        ExportUtil.exportFiles(Arrays.asList(tableHeaderNames), Arrays.asList(tableHeaderFields), list, response, "pdf", "baseLineBlackRel", "线路黑点分配信息");
    }

}
